#include "tabledatasource.h"

TableDataSource::TableDataSource(QObject *parent)
	: QAbstractTableModel(parent)
{
}

TableDataSource::~TableDataSource()
{

}

int TableDataSource::rowCount( const QModelIndex &parent /* = QModelIndex */ ) const
{
	return tableData.count() / 4;
}

int TableDataSource::columnCount( const QModelIndex &parent /* = QModelIndex */ ) const
{
	return 4;
}

QVariant TableDataSource::data( const QModelIndex &index, int role /* = Qt::DisplayRole */ ) const
{
	if (role == Qt::DisplayRole)
	{
		return QVariant(tableData[4 * index.row() + index.column()]);
	}
	else
	{
		return QVariant::Invalid;
	}
}

QVariant TableDataSource::headerData( int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole */ ) const
{
	if (role == Qt::DisplayRole && orientation == Qt::Horizontal)
	{
		switch(section)
		{
		case 0:
			return QVariant("Filename");
		case 1:
			return QVariant("Operation");
		case 2:
			return QVariant("Time");
		case 3:
			return QVariant("Compression rate");
		default:
			return QVariant::Invalid;
		}

	}
	else
	{
		return QVariant::Invalid;
	}
}

void TableDataSource::addEntry( QString& filename, QString& operation, QString& time, QString& bitrate )
{
	tableData.push_back(filename);
	tableData.push_back(operation);
	tableData.push_back(time);
	tableData.push_back(bitrate);
	int row = tableData.count() / 4;
	beginInsertRows(QModelIndex(), row - 1, row - 1);
	insertRow(row - 1);
	endInsertRows();
}
